
<template>
  <div class="filter-container">
    <div class="condition-box">
      <div class="header-box">
        <div class="title">条件配置</div>
        <el-button-group>
          <el-button size="small" type="primary" title="添加新条件" @click="conditionAdd"><i class="el-icon-yrt-jiahao"></i></el-button>
          <el-button size="small" type="primary" title="编辑选中条件" @click="conditionEdit"><i class="el-icon-yrt-bianji"></i></el-button>
          <el-button size="small" type="primary" title="删除选中条件" @click="conditionDelete"><i class="el-icon-yrt-shanchu1"></i></el-button>
        </el-button-group>
      </div>
      <div class="item-box">
        <div v-for="(item, index) in conditions" :key="index" :class="['item',currentCondition===item || currentConditionEnter===item?'enter':'']" @mouseenter="conditionMouseenter(item)" @mouseleave="conditionMouseleave(item)" @click="conditionClick(item, index)">
          <span v-if="index>0" class="txt">{{ item.join }}</span>
          <span class="txt">{{ item.left }}</span>
          <span class="txt">{{ item.operation }}</span>
          <span class="txt">{{ item.right }}</span>
        </div>
        <div v-if="!conditions.length" class="no-data">未设置条件</div>
      </div>
    </div>

    <!--条件配置对话框-->
    <el-dialog v-dialogDrag title="条件配置" :visible.sync="showConditionDialog" width="602px" append-to-body>
      <el-form ref="form" :model="formCondition" label-width="130px">
        <el-form-item v-if="((currentCondition && currentConditionIndex>0) || (!currentCondition && conditions.length>=1))" label="与上一条件关系：">
          <el-select v-model="formCondition.join" placeholder="请选择条件关系" class="w-100pc">
            <el-option v-for="(item, index) in propertyHelper.joinList" :key="index" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="属性名：">
          <el-select v-model="formCondition.left" placeholder="请选择属性" class="w-100pc">
            <el-option v-for="(item, index) in fields" :key="index" :label="item.name" :value="item.name"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="操作符：">
          <el-select v-model="formCondition.operation" placeholder="请选择操作符" class="w-100pc">
            <el-option v-for="(item, index) in propertyHelper.operatorList" :key="index" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="值表达式：">
          <el-input v-model="formCondition.right" class="w-100pc" placeholder="请输入值表达式"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showConditionDialog = false">取消</el-button>
        <el-button type="primary" @click="saveCondition">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import propertyHelper from "../components/propertyHelper.js";
import toolHelper from "../tools/toolHelper.js";

export default {
  name: "filter-condition-tab",
  props: {
    designer: {
      type: Object,
      default: () => {
        return {};
      }
    },
    fields: {
      type: Array,
      default: () => {
        return [];
      }
    }
  },
  data () {
    return {
      toolHelper,
      propertyHelper,
      formData: {
        fileName: null,
        prefix: null
      },
      // 显示条件配置对话框
      showConditionDialog: false,
      // 条件集合
      conditions: [],
      // 当前选择的条件
      currentCondition: null,
      // 条件鼠标enter
      currentConditionEnter: null,
      // 条件选中index
      currentConditionIndex: null,
      // 条件配置表单
      formCondition: {}
    };
  },
  watch: {
    "designer.cellDef": {
      handler: function () {
        this.dataInit();
      },
      deep: true
    }
  },
  mounted () {
    this.dataInit();
  },
  methods: {
    // 初始化数据
    dataInit () {
      const { cellDef } = this.designer;
      if (!cellDef || !cellDef.value) return;

      if (!Array.isArray(cellDef.value.conditions)) {
        cellDef.value.conditions = [];
      }
      this.conditions = cellDef.value.conditions;
    },
    // 条件鼠标进入
    conditionMouseenter (item) {
      this.currentConditionEnter = item;
    },
    // 条件鼠标移开
    conditionMouseleave (item) {
      this.currentConditionEnter = null;
    },
    // 条件鼠标单据
    conditionClick (item, index) {
      this.currentConditionIndex = index;
      this.currentCondition = item;
    },
    // 新增条件
    conditionAdd () {
      this.currentConditionIndex = -1;
      this.currentCondition = null;
      this.formCondition = {};
      this.showConditionDialog = true;
    },
    // 编辑条件
    conditionEdit () {
      if (!this.currentCondition) {
        this.$message.error("请选择条件项");
        return;
      }

      if (this.currentCondition.type === "property" && !this.currentCondition.left) {
        this.currentCondition.type = "current";
      }
      this.formCondition = Object.assign({}, this.currentCondition);
      this.showConditionDialog = true;
    },
    // 删除条件
    conditionDelete () {
      if (!this.currentCondition) {
        this.$message.error("请选择条件项");
        return;
      }

      this.$confirm("此操作删除该条件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          const index = this.conditions.findIndex(item => item === this.currentCondition);
          this.conditions.splice(index, 1);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 保存条件
    saveCondition () {
      if (!this.formCondition.left) {
        this.$message.error("属性名不能为空！");
        return;
      }
      if (!this.formCondition.operation) {
        this.$message.error("操作符不能为空！");
        return;
      }
      if (!this.formCondition.right) {
        this.$message.error("值表达式不能为空！");
        return;
      }

      if (!this.formCondition.join) {
        this.formCondition.join = "and";
      }
      if (this.formCondition.type === "current") {
        this.formCondition.left = null;
        this.formCondition.type = "property";
      }
      const opItem = propertyHelper.operatorList.find(item => item.value === this.formCondition.operation);
      this.formCondition.op = opItem.op;
      if (!this.formCondition.id) {
        this.formCondition.id = this.common.getGUID();
      }
      if (!this.currentCondition) {
        // 新增条件
        this.conditions.push(this.formCondition);
      } else {
        // 修改条件
        Object.assign(this.currentCondition, this.formCondition);
      }
      this.showConditionDialog = false;

      const { cellDef } = this.designer;
      cellDef.value.conditions = this.common.deepCopy(this.conditions);
    }
  }
};
</script>

<style lang="scss" scoped>
.filter-container {
  .cond-center {
    border: 1px solid #dcdfe6;
    border-radius: 4px;
    width: 320px;
    margin-left: 10px;
  }

  .header-box {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid #dcdfe6;
    padding: 10px;
    .title {
      padding: 0;
    }
  }
  .item-box {
    padding: 0;
    .item {
      padding: 5px 10px;
      line-height: 22px;
      border-bottom: 1px solid #dcdfe6;
      cursor: pointer;
      &.enter {
        background-color: #409eff;
        color: white;
      }
      .txt {
        padding-right: 5px;
      }
    }
  }
  .no-data {
    text-align: center;
    color: #666;
    padding: 20px 0;
  }
}
</style>
